package com.g5kj.coupon.ssm.controller;

import com.baidu.ueditor.ActionEnter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件上传.
 */
@RequestMapping("/fileApi")
@Controller
public class FileUploadController {

    @RequestMapping
    public @ResponseBody Map<String,Object> upload(@RequestParam("excelFile")MultipartFile file){
        Map<String,Object> res=new HashMap();
        res.put("state","SUCCESS");
        if(file!=null){
            String filePath = file.getOriginalFilename();
            File file2 = new File("file");
            if (!file2.exists()) {
                //创建临时目录
                file2.mkdir();
            }
            try {
                //文件存放的路径
                FileOutputStream fileOutputStream = new FileOutputStream(file2+"/"+filePath);
                fileOutputStream.write(file.getBytes());
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                res.put("state","fial");
            } catch (IOException e) {
                e.printStackTrace();
                res.put("state","fial");
            }
        }else{

        }



        res.put("url","wwww.baidu.com");
        return res;
    }
    @RequestMapping(value="/config")
    public void config(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("application/json");
        String rootPath = request.getSession()
                .getServletContext().getRealPath("/");

        try {
            String exec = new ActionEnter(request, rootPath).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
